<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/07/13
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\run;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use ImiApp\ApiServer\Api\Controller\user\RunBean;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;


/**
 * 统计
 * @Auth(name="token",nologin={"update_all_run","update_bill"})
 * @Controller("/run.bill/")
 */
class Bill extends RunBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];

    // /**
    //  * 获取店铺统计
    //  *
    //  * @return void
    //  * @Action
    //  */
    // public function get_bill1()
    // {
    //     $this->init();
    //     $where_com['store_id'] = $this->store_id;


    //     $yesterday_s = strtotime('yesterday');
    //     $yesterday_e = $yesterday_s + 86399;

    //     $today_s = strtotime('today');
    //     $today_e = $today_s + 86399;


    //     //今日订单数
    //     $today_order_num = Db::name('order_item')
    //         ->whereEx($where_com)
    //         ->where('createtime', '<=', $today_e)
    //         ->where('createtime', '>=', $today_s)
    //         ->count();
    //     //昨日订单数
    //     $yesterday_order_num = Db::name('order_item')
    //         ->whereEx($where_com)
    //         ->where('createtime', '<=', $yesterday_e)
    //         ->where('createtime', '>=', $yesterday_s)
    //         ->count();

    //     //今日访客数
    //     $today_storeview_num = Db::name('viewlog')
    //         ->where('viewstatus_id', '=', STORE)
    //         ->where('public_id', '=', $this->store_id)
    //         ->where('time', '<=', $today_e)
    //         ->where('time', '>=', $today_s)
    //         // ->where('user_id')//time
    //         ->count();


    //     //昨日访客数
    //     $yesterday_storeview_num = Db::name('viewlog')
    //         ->where('viewstatus_id', '=', STORE)
    //         ->where('public_id', '=', $this->store_id)
    //         ->where('time', '<=', $yesterday_e)
    //         ->where('time', '>=', $yesterday_s)
    //         // ->where('user_id')//time
    //         ->count();




    //     $data['today_order_num'] = $today_order_num ? $today_order_num : 0;
    //     $data['yesterday_order_num'] = $yesterday_order_num ? $yesterday_order_num : 0;
    //     $data['today_storeview_num'] = $today_storeview_num ? $today_storeview_num : 0;
    //     $data['yesterday_storeview_num'] = $yesterday_storeview_num ? $yesterday_storeview_num : 0;
    //     $data['store_likes_num'] = $this->store->likes;



    //     return $this->success('统计', $data);
    // }


    // /**
    //  * 获取店铺财务统计
    //  *
    //  *
    //  * @param string $typestatus_id 类型id
    //  * @param string $s_date 开始时间
    //  * @param string $e_date 结束时间
    //  * @return void
    //  * @Action
    //  */
    // public function finance($typestatus_id = null, $s_date = null, $e_date = null)
    // {


    //     $this->init();
    //     $where_com_log['store_id'] = $this->store_id;

    //     if ($typestatus_id) {
    //         $where_com_log['typestatus_id'] = $typestatus_id;
    //     }
    //     if ($s_date && $e_date) {
    //     }

    //     $where_com_order['store_id'] = $this->store_id;
    //     // $累计收入money = Db::name('order_item')
    //     //     ->whereEx($where_com_order)
    //     //     ->where('dispatch_status','=','2')//发货状态:0=未发货,1=已发货,2=已收货
    //     //     ->where('status','=','2')//订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
    //     //     ->sum();
    //     $累计收入money = Db::name('store_money_log')
    //         ->whereEx($where_com_log)
    //         ->where('status', '=', '2') //状态:0=未知,1=支出,2=收入
    //         ->sum('money');

    //     // $累计支出money =Db::name('store_money_log')
    //     //                 ->whereEx($where_com_log)
    //     //                 ->where('status','=','1')//状态:0=未知,1=支出,2=收入
    //     //                 ->sum('money');


    //     $累计收入shell = Db::name('store_shell_log')
    //         ->whereEx($where_com_log)
    //         ->where('status', '=', '2') //状态:0=未知,1=支出,2=收入
    //         ->sum('shell');

    //     // $累计支出shell =Db::name('store_shell_log')
    //     //                 ->whereEx($where_com_log)
    //     //                 ->where('status','=','1')//状态:0=未知,1=支出,2=收入
    //     //                 ->sum('shell');



    //     $累计收入score = Db::name('store_score_log')
    //         ->whereEx($where_com_log)
    //         ->where('status', '=', '2') //状态:0=未知,1=支出,2=收入
    //         ->sum('score');

    //     // $累计支出score =Db::name('store_score_log')
    //     //                 ->whereEx($where_com_log)
    //     //                 ->where('status','=','1')//状态:0=未知,1=支出,2=收入
    //     //                 ->sum('score');





    //     $子订单列表 = Db::name('order_item')
    //         ->whereEx($where_com_order)
    //         ->where('status', '=', '1') //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
    //         ->where('refund_status', '=', 0) //退款状态:-1=拒绝退款,0=无,1=申请中,2=同意
    //         ->select()->getArray();
    //     $待收入money = 0;
    //     $待收入shell = 0;
    //     $待收入score = 0;
    //     foreach ($子订单列表 as $key => $order_item) {
    //         $待收入money += $order_item['total_fee'];
    //         $待收入shell += ($order_item['item_shell_fee'] * $order_item['goods_num']);
    //         $待收入score += ($order_item['item_score_fee'] * $order_item['goods_num']);
    //     }







    //     return $this->success('', [
    //         'count_inc_money' => $累计收入money ?? 0, 'count_inc_shell' => $累计收入shell ?? 0, 'count_inc_score' => $累计收入score ?? 0,

    //         'un_finish_money' => $待收入money ?? 0, 'un_finish_shell' => $待收入shell ?? 0, 'un_finish_score' => $待收入score ?? 0
    //     ]);
    // }

    // /**
    //  * 财务管理上月本月今日
    //  *
    //  * @return void
    //  * @Action
    //  */
    // public function finance2()
    // {
    //     $this->init();
    //     $where_com['store_id'] = $this->store_id;

    //     list($当月开始, $当月结束) = $this->GetMonthTime(date('Y-m'));
    //     list($上月开始, $上月结束) = $this->GetMonthTime(date('Y-m', strtotime('last month')));
    //     $今日开始 = strtotime(date('Y-m-d'));
    //     $今日结束 = $今日开始 + 86400;
    //     $当月销售money = Db::name('store_money_log')
    //         ->whereEx($where_com)
    //         ->where('createtime', '>=', $当月开始)
    //         ->where('createtime', '<', $当月结束)
    //         ->where('typestatus_id', '=', 20)
    //         ->sum('money');

    //     $上月销售money = Db::name('store_money_log')
    //         ->whereEx($where_com)
    //         ->where('createtime', '>=', $上月开始)
    //         ->where('createtime', '<', $上月结束)
    //         ->where('typestatus_id', '=', 20)
    //         ->sum('money');


    //     $当月销售shell = Db::name('store_shell_log')
    //         ->whereEx($where_com)
    //         ->where('createtime', '>=', $当月开始)
    //         ->where('createtime', '<', $当月结束)
    //         ->where('typestatus_id', '=', 20)
    //         ->sum('shell');

    //     $上月销售shell = Db::name('store_shell_log')
    //         ->whereEx($where_com)
    //         ->where('createtime', '>=', $上月开始)
    //         ->where('createtime', '<', $上月结束)
    //         ->where('typestatus_id', '=', 20)
    //         ->sum('shell');

    //     $当月销售score = Db::name('store_score_log')
    //         ->whereEx($where_com)
    //         ->where('createtime', '>=', $当月开始)
    //         ->where('createtime', '<', $当月结束)
    //         ->where('typestatus_id', '=', 20)
    //         ->sum('score');

    //     $上月销售score = Db::name('store_score_log')
    //         ->whereEx($where_com)
    //         ->where('createtime', '>=', $上月开始)
    //         ->where('createtime', '<', $上月结束)
    //         ->where('typestatus_id', '=', 20)
    //         ->sum('score');

    //     $今日运费money = Db::name('order')
    //         ->whereEx($where_com)
    //         ->where('status', 'in', ['1', '2']) //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
    //         ->where('createtime', '>=', $今日开始)
    //         ->where('createtime', '<=', $今日结束)
    //         ->sum('dispatch_amount');

    //     $今日销售money = Db::name('order')
    //         ->whereEx($where_com)
    //         ->where('status', 'in', ['1', '2']) //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
    //         ->where('createtime', '>=', $今日开始)
    //         ->where('createtime', '<=', $今日结束)
    //         ->sum('goods_amount');

    //     $今日支付money = Db::name('order')
    //         ->whereEx($where_com)
    //         ->where('status', 'in', ['1', '2']) //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
    //         ->where('createtime', '>=', $今日开始)
    //         ->where('createtime', '<=', $今日结束)
    //         ->sum('pay_fee');

    //     $今日支付shell = Db::name('order')
    //         ->whereEx($where_com)
    //         ->where('status', 'in', ['1', '2']) //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
    //         ->where('createtime', '>=', $今日开始)
    //         ->where('createtime', '<=', $今日结束)
    //         ->sum('shell_fee');

    //     $今日支付score = Db::name('order')
    //         ->whereEx($where_com)
    //         ->where('status', 'in', ['1', '2']) //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
    //         ->where('createtime', '>=', $今日开始)
    //         ->where('createtime', '<=', $今日结束)
    //         ->sum('score_fee');




    //     return $this->success('', [
    //         'month_money' => $当月销售money ?? 0, 'month_shell' => $当月销售shell ?? 0, 'month_score' => $当月销售score ?? 0,

    //         'last_month_money' => $上月销售money ?? 0, 'last_month_shell' => $上月销售shell ?? 0, 'last_month_score' => $上月销售score ?? 0,

    //         'today_count_money' => $今日销售money+$今日运费money ?? 0, 'today_pay_money' => $今日支付money ?? 0, 'today_pay_shell' => $今日支付shell ?? 0, 'today_pay_score' => $今日支付score ?? 0
    //     ]);
    // }


    // /**
    //  * 获取订单列表
    //  *
    //  * @param string $status 1=待结算,2=已到账
    //  * @param integer $page 1
    //  * @param integer $limit 10
    //  * @param string $s_date 开始时间
    //  * @param string $e_date 结束时间
    //  * @Action
    //  * @return void
    //  */
    // public function get_order_list(?string $status=null, ?int $page = 1, ?int $limit = 10, $s_date = null, $e_date = null)
    // {
    //     $page = $page ?: 1;
    //     $limit = $limit ?: 10;
    //     $this->init();
    //     $where_com['store_id'] = $this->store_id;

    //     if($status){
    //         $where_com['status'] = $status;  
    //     }else{
    //         $where_com['status'] = ['in',['1', '2']];  
    //     }

    //     if($s_date && $e_date){
    //         $where_com['createtime'] = ['>=',strtotime($s_date)];
    //         $list = Db::name('order_item')
    //         ->whereEx($where_com)
    //         ->where('createtime','>=',strtotime($s_date))
    //         ->where('createtime','<=',strtotime($e_date))
    //         // ->where('status', 'in', ) //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成'
    //         ->page($page,$limit)
    //         ->select()->getArray();
    //     }else{
    //         $list = Db::name('order_item')
    //             ->whereEx($where_com)
    //             // ->where('status', 'in', ['1', '2']) //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成'
    //             ->page($page,$limit)
    //             ->select()->getArray();
    //     }

    //     foreach ($list as $key => &$val) {
    //        $val['date'] = date('Y-m-d H:i',$val['createtime']);
    //     }

    //     return $this->success('',$list);

    // }




    /**
     * 获取统计邀请
     * @Action
     * @return void
     */
    public function get_count()
    {
        $this->init();
        $where['user_id'] = $user_id = $this->auth->user()->id;
        $run_id = $this->auth->user()->run_id;

        $我邀请的会员数 = Db::name('user')->where('pid', '=', $user_id)->count();
        $invite_user_ids = Db::name('user')->where('pid', '=', $user_id)->column('id');
        $store_num = Db::name('store')->where('user_id', 'in', $invite_user_ids)->count();
        $辅导员店铺 = Db::name('store')->where('pid', '=', $user_id)->count();
        $间接会员数 = 0;
        if ($invite_user_ids) {
            foreach ($invite_user_ids as $key => $id) {
                $temp = Db::name('user')->where('pid', 'in', $invite_user_ids)->count();
                $间接会员数 = $间接会员数 + $temp ? $temp : 0;
            }
        }

        $run_store_num = Db::name('store')->where('run_id', '=', $run_id)->count();
        $我运营中心的会员数 = Db::name('user')->where('run_id', '=', $run_id)->count();
        $我运营中心的会员数 = $我运营中心的会员数 ? $我运营中心的会员数 : 0;

        $我邀请的会员数 = $我邀请的会员数 ? $我邀请的会员数 : 0;
        $间接会员数 = $间接会员数 ? $间接会员数 : 0;
        $store_num = $store_num ? $store_num : 0;
        $run_store_num = $run_store_num ? $run_store_num : 0;

        $辅导员店铺 =  $辅导员店铺 ? $辅导员店铺 : 0;
        $data['run_user_num'] = $我运营中心的会员数;
        $data['user_num'] = $我邀请的会员数;
        $data['store_num'] = $store_num;

        $data['run_store_num'] = $run_store_num;
        $data['indirect_user_num'] = $间接会员数;
        $data['coach_store_num'] =  $辅导员店铺;
        // $data['out'] = Db::name('signlog')->whereEx($where)->where('status','1')->sum('signlog');  
        // $data['this_month_inc'] = Db::name('signlog')->whereEx($where)->whereTime('createtime', 'm')->where('status','2')->sum('signlog');  
        // $data['this_month_dec'] = Db::name('signlog')->whereEx($where)->whereTime('createtime', 'm')->where('status','1')->sum('signlog');  
        return $this->success('统计', $data);
    }



    /**
     * 获取邀请会员列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $search 搜索
     * @return void
     * @Action
     */
    public function get_invite_user_list(?int $page = 1, ?int  $limit = 10, ?string $search = null)
    {
        $this->init();
        $page = $page ? $page : 1;
        $limit = $limit ? $limit : 10;

        $data['pid'] = $this->auth->user()->id;
        if ($search) {
            $data['nickname'] = ['like', "%" . $search . "%"];
        }

        $list = Db::name('user')->orderRaw('id desc')->whereEx($data)->fieldRaw('nickname,avatar,mobile,id,createtime,set_id,wechat_number')->page($page, $limit)->select()->getArray();

        if ($list)
            foreach ($list as $key => &$val) {

                $val['mobile'] = $this->HideMobile($val['mobile']);
                // $this->ChangeFieldData($val);
                $val['date'] = date('Y-m-d H:i', $val['createtime']);
                $val['f'] = 0;
                $val['set_name'] = Db::name('set')->where('id', '=', $val['set_id'])->value('name');
                $val['order'] = Db::name('order_item')
                    ->where('user_id', '=', $val['id'])
                    ->where('status', '=', '2') //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
                    ->count();
            }
        return $this->success('ok', $list);
    }

    /**
     * 获取邀请店铺列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $search 搜索
     * @return void
     * @Action
     */
    public function get_invite_store_list(?int $page = 1, ?int  $limit = 10, ?string $search = null)
    {
        $this->init();
        $page = $page ?: 1;
        $limit = $limit ?: 10;


        $invite_user_ids = Db::name('user')->where('pid', '=', $this->auth->user()->id)->column('id');
        if ($search) {
            $data['name'] = ['like', "%" . $search . "%"];
        }
        $data['user_id'] = ['in', $invite_user_ids];

        $list = Db::name('store')->whereEx($data)->page($page, $limit)->orderRaw('id desc')->select()->getArray();
        // ->orderRaw('id desc')->field('nickname,mobile,id,createtime')->page($page, $limit)->select()->getArray();
        if ($list)
            foreach ($list as $key => &$val) {
                // $val['mobile'] = $this->HideMobile($val['mobile']);
                // $this->ChangeFieldData($val);
                $val['date'] = date('Y-m-d H:i', $val['createtime']);
                $coach = Db::name('user')->where('id', '=', $val['pid'])->fieldRaw('nickname,avatar,mobile,id,createtime,set_id,wechat_number')->find();
                if ($coach) {
                    $coach['set_name'] = Db::name('set')->where('id', '=', $coach['set_id'])->value('name');
                }
                $val['coach'] = $coach;
            }
        return $this->success('ok', $list);
    }


    /**
     * 获取辅导员邀请店铺列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $search 搜索
     * @return void
     * @Action
     */
    public function get_coach_store_list(?int $page = 1, ?int  $limit = 10, ?string $search = null)
    {
        $this->init();
        $page = $page ?: 1;
        $limit = $limit ?: 10;

        if ($search) {
            $data['name'] = ['like', "%" . $search . "%"];
        }
        $data['pid'] = $this->auth->user()->id;

        $list = Db::name('store')->whereEx($data)->page($page, $limit)->orderRaw('id desc')->select()->getArray();

        if ($list)
            foreach ($list as $key => &$val) {
                // $val['mobile'] = $this->HideMobile($val['mobile']);
                // $this->ChangeFieldData($val);
                $val['date'] = date('Y-m-d H:i', $val['createtime']);
                $coach = Db::name('user')->where('id', '=', $val['pid'])->fieldRaw('nickname,avatar,mobile,id,createtime,set_id,wechat_number')->find();
                if ($coach) {
                    $coach['set_name'] = Db::name('set')->where('id', '=', $coach['set_id'])->value('name');
                }
                $val['coach'] = $coach;
            }
        return $this->success('ok', $list);
    }





    /**
     * 获取运营中心下面的会员列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $search 搜索
     * @param integer $set_id 等级级以上
     * @return void
     * @Action
     */
    public function get_user_list(?int $page = 1, ?int  $limit = 10, ?string $search = null, ?int $set_id = 1)
    {
        $this->init();
        $page = $page ? $page : 1;
        $limit = $limit ? $limit : 10;

        $data['run_id'] = $this->run_id;
        if ($search) {
            $data['mobile'] = ['like', "%" . $search];
        }

        $list = Db::name('user')->orderRaw('id desc')->whereEx($data)->fieldRaw('nickname,avatar,mobile,id,createtime,set_id,wechat_number')->where('set_id', '>=', $set_id ?? 1)->page($page, $limit)->select()->getArray();

        if ($list)
            foreach ($list as $key => &$val) {

                // $val['mobile'] = $this->HideMobile($val['mobile']);
                // $this->ChangeFieldData($val);
                $val['date'] = date('Y.m.d H:i', $val['createtime']);
                // $val['f'] = 0;
                $val['set_name'] = Db::name('set')->where('id', '=', $val['set_id'])->value('name');
                // $val['order'] = Db::name('order_item')
                //         ->where('user_id','=',$val['id'])
                //         ->where('status','=','2')//订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
                //         ->count();
                $val['last_month_commission'] = $this->用户上月提成($val['id']);
                $val['month_commission'] = $this->用户某月提成($val['id']);
            }
        return $this->success('ok', $list);
    }


    /**
     * 获取运营中心系统设置的会员列表
     *
     * @return void
     * @Action
     */
    public function get_sys_user_list()
    {
        $this->init();

        $data['run_id'] = $this->run_id;


        $list = Db::name('run_bonus_user_sys')->whereEx($data)->select()->getArray();
        foreach ($list as $key => &$val) {

            $val['user'] = Db::name('user')
                ->orderRaw('id desc')
                ->where('id', '=', $val['user_id'])
                ->fieldRaw('nickname,avatar,mobile,id,createtime,set_id')
                ->find();
            if ($val['user']) {
                $val['set_name'] = $this->getSetName($val['user']['set_id']);
                $val['user']['date'] = date('Y.m.d H:i', $val['user']['createtime']);

                // $val['last_month_commission'] = $this->用户上月提成($val['user_id']);
                // $val['month_commission'] = $this->用户某月提成($val['user_id']);
            }
        }
        $bonus_ratio = Db::name('run_bonus_ratio')->where('run_id', '=', $this->run_id)->orderRaw('weigh asc')->select()->getArray();
        return $this->success('', ['list' => $list, 'fasten_ratio' => $this->获取运营中心固定用户占比($this->run_id), 'bonus_ratio' => $bonus_ratio]);
    }



    /**
     * 获取运营中心团队业绩会员列表
     *
     * @return void
     * @Action
     */
    public function get_count_user_list($date = null)
    {
        $this->init();
        if (!$date)
            $date = date('Y-m-d');
        $data['run_id'] = $this->run_id;
        var_dump('运营中心id' . $this->run_id);


        $user_sys = Db::name('run_bonus_user_sys')->whereEx($data)->select()->getArray();
        foreach ($user_sys as $key => &$val) {

            $val['user'] = Db::name('user')
                ->orderRaw('id desc')
                ->where('id', '=', $val['user_id'])
                ->fieldRaw('nickname,avatar,mobile,id,createtime,set_id')
                ->find();
            if ($val['user']) {
                $val['set_name'] = $this->getSetName($val['user']['set_id']);
                $val['user']['date'] = date('Y.m.d H:i', $val['user']['createtime']);

                $val['last_month_commission'] = $this->用户上月提成($val['user_id']);
                $val['month_commission'] = $this->用户某月提成($val['user_id']);
                $val['inc_ratio'] = $this->获取增长比例($val['month_commission'], $val['last_month_commission']);
                $val['now_commission'] = $this->运营中心获取用户分配当前收益($this->run_id, $date, $val['ratio']);
            }
        }

        $user_run = Db::name('run_bonus_user')->whereEx($data)->select()->getArray();
        foreach ($user_run as $key => &$val) {

            $val['user'] = Db::name('user')
                ->orderRaw('id desc')
                ->where('id', '=', $val['user_id'])
                ->fieldRaw('nickname,avatar,mobile,id,createtime,set_id')
                ->find();
            if ($val['user']) {
                $val['set_name'] = $this->getSetName($val['user']['set_id']);
                $val['user']['date'] = date('Y.m.d H:i', $val['user']['createtime']);

                $val['last_month_commission'] = $this->用户上月提成($val['user_id']);
                $val['month_commission'] = $this->用户某月提成($val['user_id']);
                $val['inc_ratio'] = $this->获取增长比例($val['month_commission'], $val['last_month_commission']);

                $val['now_commission'] = $this->运营中心获取用户分配当前收益($this->run_id, $date, $val['ratio']);
            }
        }

        // 提取年龄字段到新数组
        $ages = array_column($user_run, 'inc_ratio');

        // 按年龄字段降序排序
        array_multisort($ages, SORT_DESC, $user_run);

        // 显示排序后的结果
        foreach ($user_run as $key => &$user) {
            $user['ratio'] = Db::name('run_bonus_ratio')->where('run_id', '=', $this->run_id)->where('weigh', '=', $key + 1)->value('ratio');
        }


        return $this->success('', ['user_sys' => $user_sys, 'user_run' => $user_run,]);
    }




    /**
     * 添加奖金用户
     *
     * @param string $user_ids 用户id
     * @Action
     * @return void
     */
    public function add_bonus_user($user_ids)
    {
        $this->init();

        $user_ids = explode(',', $user_ids);

        foreach ($user_ids as $key => $user_id) {
            $data['user_id'] = $user_id;
            $data['run_id']  = $this->run_id;
            $res = Db::name('run_bonus_user')->whereEx($data)->find();
            if ($res) {
                return $this->error('id' . $user_id . '该用户已存在', $user_id);
            }
        }

        foreach ($user_ids as $key => $user_id) {
            $data['user_id'] = $user_id;
            $data['run_id']  = $this->run_id;
            $res = Db::name('run_bonus_user')->whereEx($data)->find();
            if ($res) {
                return $this->error('id' . $user_id . '该用户已存在', $user_id);
            }
            Db::name('run_bonus_user')->insert($data);
        }




        return $this->success('添加成功');
    }



    /**
     * 更新用户分配占比
     *
     * @param string $json [{"weigh":1,"ratio":12.1}]
     * @Action
     * @return void
     */
    public function update_bonus_ratio($json)
    {
        $this->init();

        $str = htmlspecialchars_decode($json);
        var_dump($str);
        //run_bonus_ratio
        $json = json_decode($str, true);

        $总占比 = 0;
        foreach ($json as $key => $v) {
            $v['ratio'] = $v['ratio'] + $总占比;
        }
        $固定占比 = $this->获取运营中心固定用户占比($this->run_id);

        if ($总占比 + $固定占比 > 100) {
            return $this->error('占比大于100');
        }

        if (date('n') > 15) {
            return $this->error('只能每月15号前修改');
        }
        foreach ($json as $key => $v) {
            // $v['ratio'] = $v['ratio'];
            unset($data);
            $data['weigh'] = $v['weigh'];
            $res = Db::name('run_bonus_ratio')->where('run_id', '=', $this->run_id)->where('weigh', '=', $v['weigh'])->find();
            $data['ratio'] = $v['ratio'];
            if ($res) {
                Db::name('run_bonus_ratio')->where('id', '=', $res['id'])->update($data);
            } else {
                $data['run_id'] = $this->run_id;
                Db::name('run_bonus_ratio')->insert($data);
            }
        }

        return $this->success('更新成功');
    }




    /**
     * 删除奖金用户
     *
     * @param int $user_id 用户id
     * @Action
     * @return void
     */
    public function del_bonus_user($user_id)
    {
        $this->init();

        $data['user_id'] = $user_id;
        $data['run_id']  = $this->run_id;
        $res = Db::name('run_bonus_user')->whereEx($data)->find();
        if (!$res) {
            return $this->error('该用户不存在', -1);
        }
        Db::name('run_bonus_user')->whereEx($data)->delete();
        return $this->success('删除成功');
    }


    /**
     * 获取奖金用户列表
     *
     * @Action
     * @return void
     */
    public function get_bonus_user_list($status = null)
    {
        $this->init();

        $data['run_id']  = $this->run_id;
        $list = Db::name('run_bonus_user')->whereEx($data)->select()->getArray();
        foreach ($list as $key => &$val) {

            $val['user'] = Db::name('user')
                ->orderRaw('id desc')
                ->where('id', '=', $val['user_id'])
                ->fieldRaw('nickname,avatar,mobile,id,createtime,set_id')
                ->find();
            if ($val['user']) {
                $val['set_name'] = $this->getSetName($val['user']['set_id']);
                $val['user']['date'] = date('Y.m.d H:i', $val['user']['createtime']);

                $val['last_month_commission'] = $this->用户上月提成($val['user_id']);
                $val['month_commission'] = $this->用户某月提成($val['user_id']);
            }
        }
        return $this->success('', $list);
    }


    /**
     * 运营中心统计
     *
     * @param string $date 日期
     * @Action
     * @return void
     */
    public function count(?string $date = null)
    {
        $this->init();

        $data['xiaofei_count'] = 0; //消费总额
        $data['xiaofei_zhanbi'] = 0; //占比
        $data['xiaofei_zengzhang'] = 0; //增长
        $data['xiaofei_dangqian'] = 0; //当前



        $data['yingye_count'] = 0; //营业总额
        $data['yingye_zhanbi'] = 0; //占比
        $data['yingye_zengzhang'] = 0; //增长
        $data['yingye_dangqian'] = 0; //当前

        $data['extend_quota'] = 0; //推广额度
        $data['residue_quota'] = 0; //剩余额度

        $data['last_month_uncommission'] = 0; //上月待分配
        $data['month_count'] = 0; //本月总计
        // $data['xiaofei_dangqian'] = 0; //当前

        if (!$date) {
            $date = date('Y-m-d');
        }

        list($当月开始, $当月结束) = $this->GetMonthTime($date);
        list($上月开始, $上月结束) = $this->GetMonthTime(date('Y-m', strtotime('last month', strtotime($date))));
        $当月结束 = strtotime($date) + 86399;

        $data['xiaofei_count'] = $消费总额 = $this->运营中心下用户消费额($this->run_id);

        $data['yingye_count'] = $营业总额 = $this->运营中心下店铺销售额($this->run_id);
        $data['last_month_uncommission'] = $this->上月累计激励金($this->run_id, $date);


        $data['xiaofei_dangqian'] = $this->当前收益_消费($this->run_id, $date);
        $data['yingye_dangqian'] = $this->当前收益_营业($this->run_id, $date);

        $data['xiaofei_zengzhang'] = $this->消费增长率($this->run_id, $date);
        $data['yingye_zengzhang'] = $this->营业增长率($this->run_id, $date);




        return $this->success('', $data);
    }





    /**
     * 更新运营中心统计
     *
     * @Action
     * 
     *
     * @param string $date 日期
     * @return void
     */
    public function update_all_run($date = null)
    {
        $list = Db::name('run')->select()->getArray();

        foreach ($list as $key => $val) {
            // var_dump($val['id']);
            $data = $this->update_bill($date, $val['id'], true);
        }

        // var_dump($list);

    }




    /**
     * 运营中心测试统计
     *
     * @Action
     * 
     *
     * @param string $date 日期
     * @param integer $run_id 运营中心 
     * @return void
     */
    public function update_bill($date = null, $run_id = 1, $self = false)
    {
        $run_id = 1;
        if (!$date)
            $date = date('Y-m-d');

        $data['run_id'] = $run_id;
        $time = strtotime($date) + 1;
        $data['year'] = date('Y', $time);
        $data['m'] = date('n', $time);
        $data['day'] = date('d', $time);
        $data['date'] = date('Y-m-d', $time);
        $res = Db::name('run_excite_money_bill')->whereEx($data)->find();

        $data['time']  = $time;
        $com_data = $data;
        $data['updatetime'] = time();




        $data['last_month_total_excite_money'] = $this->上月累计激励金($run_id, $date);
        $data['admin_set_excite_money_send_ratio'] = $this->后台设置的激励金发放比例($run_id, $date);
        $data['now_excite_money_send_ratio'] = $this->当前激励金发放比例($run_id, $date);

        $data['unallocate_excite_money'] = $this->待分配激励金($run_id, $date);
        $data['month_total_total_excite_money'] = $this->本月累计激励金($run_id, $date);

        $data['surplus_excite_money'] = $this->激励金结余($run_id, $date);
        $data['last_month_total_consume'] = $this->上月消费总额($run_id, $date);


        $data['month_total_consume'] = $this->本月消费总额($run_id, $date);
        $data['consume_ratio'] = $this->消费占比($run_id, $date);

        $data['consume_inc_ratio'] = $this->消费增长率($run_id, $date);


        $data['consume_send_ratio'] = $this->获取消费发放比例($run_id, $date); //_N
        $data['now_income_consume'] = $this->当前收益_消费($run_id, $date);

        $data['last_month_total_trade'] = $this->上月营业总额($run_id, $date);
        $data['month_total_trade'] = $this->本月营业总额($run_id, $date);

        $data['trade_ratio'] = $this->营业占比($run_id, $date);
        $data['trade_inc_ratio'] = $this->营业增长率($run_id, $date);
        $data['trade_send_ratio'] = $this->获取营业发放比例($run_id, $date); //_T

        $data['now_income_trade'] = $this->当前收益_营业($run_id, $date);
        if ($res) {
            Db::name('run_excite_money_bill')->where('id', '=', $res['id'])->update($data);
            $msg = "更新成功";
        } else {
            Db::name('run_excite_money_bill')->insert($data);
            $msg = "加入成功";
        }

        $sys_user_list = $this->获取系统分配人员列表($run_id);
        foreach ($sys_user_list as $key => &$val) {
            $val['money'] = $this->运营中心获取用户分配当前收益($run_id, $date, $val['ratio']);


            $where_com = $com_data;
            $where_com['user_id'] = $val['user_id'];
            $where_com['status'] = 'sys'; //状态:sys=系统添加,run=运营中心添加
            $where_com['bonus_ratio'] = $val['ratio'];
            $where_com['bonus'] = $val['money'];
            var_dump($where_com);

            $res = Db::name('run_excite_money_bill_user')->whereEx($com_data)->find();
            if ($res) {
                $where_com['updatetime'] = time();

                Db::name('run_excite_money_bill_user')->where('id', '=', $res['id'])->update($where_com);
            } else {
                $where_com['time'] = $time;
                $where_com['updatetime'] = time();
                Db::name('run_excite_money_bill_user')->insert($where_com);
            }
        }

        $user_list = $this->获取分配人员和比例列表($run_id);
        foreach ($user_list as $key => &$val) {
            $val['money'] = $this->运营中心获取用户分配当前收益($run_id, $date, $val['ratio']);


            $where_com = $com_data;
            $where_com['user_id'] = $val['user_id'];
            $where_com['status'] = 'run'; //状态:sys=系统添加,run=运营中心添加
            $where_com['bonus_ratio'] = $val['ratio'];
            $where_com['bonus'] = $val['money'];
            var_dump($where_com);

            $res = Db::name('run_excite_money_bill_user')->whereEx($com_data)->find();
            if ($res) {
                $where_com['updatetime'] = time();

                Db::name('run_excite_money_bill_user')->where('id', '=', $res['id'])->update($where_com);
            } else {
                $where_com['time'] = $time;
                $where_com['updatetime'] = time();
                Db::name('run_excite_money_bill_user')->insert($where_com);
            }
        }



        // var_dump($data);
        if (!$self)
            return $this->success($msg, ['data' => $data, 'sys_user_list' => $sys_user_list, 'user_list' => $user_list]);

        return $data;
    }
}
